package com.Dao.impl;

import com.Dao.userDao;
import com.utils.Result;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;
import com.pojo.User;

import javax.jws.soap.SOAPBinding;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Repository
public class userDaoImpl implements userDao {
    private static List<User> users=null;
    static {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            //模拟数据库
            users=new ArrayList<>();
            users.add(new User(1,"zk","123456",simpleDateFormat.parse("2019-11-01"),"管理员",new String[]{"查询","增加","删除","修改"}));
            users.add(new User(2,"zrs","123456",simpleDateFormat.parse("2019-11-01"),"经理",new String[]{"查询","增加","修改"}));
            users.add(new User(3,"hxy","123456",simpleDateFormat.parse("2001-01-01"),"普通用户",new String[]{"查询","修改"}));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User login(String username, String password) {
            for(User user:users){
                System.out.println(username.equals(user.getUsername()));
                if(username.equals(user.getUsername())&&password.equals(user.getPassword())){
                    return user;
                };
        }
        return null;
    }
    @Override
    public Boolean findUserByUsername(String username) {
        for(User user:users){
            if(username.equals(user.getUsername())){
                 return true;
            }
        }
        return false;
    }

    @Override
    public User findUserById(Integer id) {
//        for(User user:users){
//            if(id.equals(user.getId())){
//                return user;
//            }
//        }
//        return null;
        //获取bean对象
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("classpath:bean.xml");
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);
        return user;
    }

    @Override
    public Boolean addUser(User user) {
        System.out.println(user);
        if(users.add(user)){
            return true;
        }else{return false;}
    }

    @Override
    public Boolean updateUser(User user) {
        for(User user1:users){
            if(user1.getId()==user.getId()){
                user1.setUsername(user.getUsername());
                user1.setPassword(user.getPassword());
                user1.setBirthdate(user.getBirthdate());
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean deleteUser(Integer id) {
        for(User user:users){
            if(id.equals(user.getId())){
                users.remove(user);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<User> findAllUser() {
        return users;
    }

}
